/**
 * 替代关系Info*E Task类
 * 
 * @author 魏文杰 
 * @Version 1.0
 * @Date 2013-10-08
 */


package ext.generic.erp.task;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import ext.generic.erp.attributes.AttributeUtil;
import ext.generic.erp.bean.AlternativeMaterial;
import ext.generic.erp.config.ERPPropertiesUtil;
import ext.generic.erp.util.IEXMLUtil;
import ext.generic.erp.util.IntegrationConstant;
import ext.generic.erp.util.PDMIntegrationLogUtil;
import ext.generic.erp.common.CommonUtil;

import com.infoengine.SAK.Task;
import com.infoengine.util.IEException;
import com.infoengine.object.factory.Group;

public class AlternativeMaterialIETask {
	
	private static String clazz = AlternativeMaterialIETask.class.getName() ;
	
	private static boolean verbose = PDMIntegrationLogUtil.verbose ;
	
	private static String ieXMLFileQuery = null ;
	private static String ieXMLFileCreate = null;
	private static String ieXMLFileUpdate = null ;
	private static String ieXMLFileDelete = null ;
	
	private static IEXMLUtil ieXMLUtil = new IEXMLUtil();
	
	static {
		ieXMLFileQuery = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.REPLACE_PART_QUERY_IE_XML_FILE);
		ieXMLFileCreate = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.REPLACE_PART_CREATE_IE_XML_FILE);
		ieXMLFileUpdate = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.REPLACE_PART_UPDATE_IE_XML_FILE);
		ieXMLFileDelete = (String) ERPPropertiesUtil.getInstance().readProperties().get(IntegrationConstant.REPLACE_PART_DELETE_IE_XML_FILE);
		
		initialCheck();
	}
	
	private static void initialCheck(){
		if(ieXMLFileQuery != null){
			ieXMLFileQuery = ieXMLFileQuery.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Query Config File : " + ieXMLFileQuery , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Query Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileCreate != null){
			ieXMLFileCreate = ieXMLFileCreate.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Create Config File : " + ieXMLFileCreate , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Create Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileUpdate != null){
			ieXMLFileUpdate = ieXMLFileUpdate.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Update Config File : " + ieXMLFileUpdate , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Update Config File : NULL" , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
		
		if(ieXMLFileDelete != null){
			ieXMLFileDelete = ieXMLFileDelete.trim() ;
			
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : " + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "Delete Config File : NULL" + ieXMLFileDelete , IntegrationConstant.LOG_LEVEL_ERROR ) ;
		}
	}
	
	/**
	 * 查询中间表中的替代件关系
	 * 
	 * @param material
	 * @return
	 */
	public static Group query( AlternativeMaterial material ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In query( AlternativeMaterial material )......") ;
		
		Group group = null ;
		
		//创建任务
		Task task = new Task( ieXMLFileQuery );
		
		IETaskCommonUtil.addWhere( material , task ) ;

		//执行任务
		try {
			PDMIntegrationLogUtil.printLog(clazz, "准备执行替代关系信息查询Task......") ;
			
			task.invoke();
			
			PDMIntegrationLogUtil.printLog(clazz, "替代关系信息查询Task执行完成......") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException..." , IntegrationConstant.LOG_LEVEL_ERROR) ;
			PDMIntegrationLogUtil.printLog(clazz, e.toString() , IntegrationConstant.LOG_LEVEL_ERROR) ;
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException..." , IntegrationConstant.LOG_LEVEL_ERROR) ;
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage() , IntegrationConstant.LOG_LEVEL_ERROR) ;
			
			e.printStackTrace();
		}
		
		//获取返回值
		group =  task.getGroup("QueryObjects");
		
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out query( AlternativeMaterial material )......") ;
		
		return group ;
	}
	
	/**
	 * 判断中间表是否有替代关系
	 * 
	 * @param material
	 * @return
	 */
	public static boolean hasObject( AlternativeMaterial material ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In hasObject( AlternativeMaterial material )......") ;
		
		Group group =   query( material ) ;
		
		boolean hasAlternativeMaterial = IETaskCommonUtil.hasObject( group ) ;
		
		PDMIntegrationLogUtil.printLog(clazz, "hasAlternativeMaterial = " + hasAlternativeMaterial ) ;
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out hasObject( AlternativeMaterial material )......") ;
		
		return hasAlternativeMaterial ;
	}
	
	/**
	 * 向中间表添加替代件关系
	 * 
	 * @param material
	 */
	public static void add( AlternativeMaterial material ){
		add( material , "") ;
	}
	
	/**
	 * 向中间表添加替代件关系
	 * 
	 * @param material
	 * @param releaseTime
	 */
	public static void add( AlternativeMaterial material , String releaseTime ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In add( AlternativeMaterial material , String releaseTime )......") ;
		
		boolean hasAlternativeMaterial = hasObject( material ) ;
		
		PDMIntegrationLogUtil.printLog(clazz, "hasAlternativeMaterial = " + hasAlternativeMaterial ) ;
		
		if( hasAlternativeMaterial ){
			PDMIntegrationLogUtil.printLog(clazz, "执行更新替代件信息操作") ;
			
			update( material , releaseTime ) ;
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "执行创建替代件信息操作") ;
			
			create( material , releaseTime ) ;
		}
		
		PDMIntegrationLogUtil.printLog(clazz, "Existed Out add( AlternativeMaterial material , String releaseTime )......") ;
	}
	
	/**
	 * 从中间表中删除替代件的信息
	 * 
	 * @param material
	 */
	public static void delete( AlternativeMaterial material ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In delete( AlternativeMaterial material )......") ;
		
		if( material == null ){
			PDMIntegrationLogUtil.printLog(clazz, "delete Method, AlternativeMaterial is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileDelete);
		
		IETaskCommonUtil.addWhereForDelete( material , task );
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行替代关系信息删除操作") ;
			task.invoke();
			PDMIntegrationLogUtil.printLog(clazz, "删除替代关系信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out delete( AlternativeMaterial material )......") ;
		}
	}
	
	/**
	 * 在中间表中创建替代关系
	 * 
	 * @param material
	 * @param releaseTime
	 */
	protected static void create( AlternativeMaterial material , String releaseTime ) {
		PDMIntegrationLogUtil.printLog(clazz, "Entering In create( AlternativeMaterial material , String releaseTime )......") ;
		
		if( material == null ){
			PDMIntegrationLogUtil.printLog(clazz, "create Method, AlternativeMaterial is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml( IETaskCommonUtil.TASK_FILE_DIRECOTRY +ieXMLFileCreate );
		
		if(verbose){
			PDMIntegrationLogUtil.printLog(clazz, CommonUtil.getMapStringValue( fieldMap ) , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}
		
		Task task = new Task( ieXMLFileCreate );

		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			
			String attributeValue = AttributeUtil.getAttribute( material , attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		IETaskCommonUtil.addProcessStatus( task , releaseTime );
		
		IETaskCommonUtil.updatePDMWritebackStatus(task);
		
		if(verbose){			
			PDMIntegrationLogUtil.printLog(clazz, IETaskCommonUtil.getTaskParams(task) );
		}
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行替代关系信息创建任务") ;
			
			task.invoke();
			
			PDMIntegrationLogUtil.printLog(clazz, "创建替代关系信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out create( AlternativeMaterial material , String releaseTime )......") ;
		}
	}
	
	/**
	 * 更新中间表中的替代关系
	 * 
	 * @param material
	 * @param releaseTime
	 */
	protected static void update( AlternativeMaterial material , String releaseTime ){
		PDMIntegrationLogUtil.printLog(clazz, "Entering In update( AlternativeMaterial material , String releaseTime )......") ;
		
		if( material == null ){
			PDMIntegrationLogUtil.printLog(clazz, "update Method, AlternativeMaterial is NULL", IntegrationConstant.LOG_LEVEL_WARN);
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml( IETaskCommonUtil.TASK_FILE_DIRECOTRY +ieXMLFileUpdate );
		
		if(verbose){
			PDMIntegrationLogUtil.printLog(clazz, CommonUtil.getMapStringValue( fieldMap ) , IntegrationConstant.LOG_LEVEL_NORMAL ) ;
		}
		
		//创建任务
		Task task = new Task(ieXMLFileUpdate);
		
		Iterator<String> ite = fieldMap.keySet().iterator() ;
		
		//根据属性映射关系，发布属性值
		while(ite.hasNext()){
			String dbField = ite.next();
			String attributeId = fieldMap.get( dbField );
			
			String attributeValue = AttributeUtil.getAttribute( material , attributeId ) ;
			
			task.addParam( attributeId , attributeValue );
		}
		
		IETaskCommonUtil.addProcessStatus( task , releaseTime );
		
		IETaskCommonUtil.updatePDMWritebackStatus(task);
		
		IETaskCommonUtil.addWhere(material, task) ;
		
		if(verbose){			
			PDMIntegrationLogUtil.printLog(clazz, IETaskCommonUtil.getTaskParams(task) );
		}
		
		try {
			PDMIntegrationLogUtil.printLog(clazz, "执行替代关系信息更新任务") ;
			
			task.invoke();
			
			PDMIntegrationLogUtil.printLog(clazz, "更新替代关系信息任务结束") ;
		} catch (IEException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IEException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.toString(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		} catch (IOException e) {
			PDMIntegrationLogUtil.printLog(clazz, "Throws IOException", IntegrationConstant.LOG_LEVEL_ERROR);
			PDMIntegrationLogUtil.printLog(clazz, e.getMessage(), IntegrationConstant.LOG_LEVEL_ERROR);
			
			e.printStackTrace();
		}finally{
			PDMIntegrationLogUtil.printLog(clazz, "Existed Out update( AlternativeMaterial material , String releaseTime )......") ;
		}
	}
}
